package org.pasta.web.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.web.servlet.ModelAndView;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public abstract class AJAXController extends POSController {

	//public static String LIST_DATA_REQ_ATTR = "listData";
	public static String JSON_DATA_REQ_ATTR = "JSONData";

	ModelAndView listData = new ModelAndView("listData");

	List dataL = new ArrayList();

	protected ModelAndView process() {
		dataL = inquiryListData();
		String jsonString = null;

		ObjectMapper mapper = new ObjectMapper();
		try {
			jsonString = mapper.writeValueAsString(dataL);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		//request.setAttribute(LIST_DATA_REQ_ATTR, dataL);
		request.setAttribute(JSON_DATA_REQ_ATTR, jsonString);
		return listData;
	}

	protected abstract List inquiryListData();

}
